﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;

namespace 天离安全_Mssql渗透系统
{
    public partial class Proxy : Form
    {
        private const string ConfigFilePath = "Appactions.json";
        public Proxy()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 测试socks4代理服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                Services.T_Proxy.JsonNode_Socks4(Api.T_Client_Mssql_Api.C1);
                // 执行socks4 代理是否连接成功
                Services.T_Client_Mssql.TestSocks4(Api.T_Client_Mssql_Api.C1);
                string[] dataArr = Api.T_Client_Mssql_Api.C_Status.ToString().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var data in dataArr)
                {
                    if (data.Contains("代理连接成功"))
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接成功" + "\r\n" + data + "\r\n");
                    }
                    else
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接失败" + "\r\n" + data + "\r\n");
                    }
                }
            }
        }
        /// <summary>
        /// 测试socks5代理服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox4.Text) || string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                Services.T_Proxy.JsonNode_Socks5(Api.T_Client_Mssql_Api.C1);
                // 执行socks4 代理是否连接成功
                Services.T_Client_Mssql.TestSocks5(Api.T_Client_Mssql_Api.C1);
                string[] dataArr = Api.T_Client_Mssql_Api.C_Status.ToString().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var data in dataArr)
                {
                    if (data.Contains("代理连接成功"))
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接成功" + "\r\n" + data + "\r\n");
                    }
                    else
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接失败" + "\r\n" + data + "\r\n");
                    }
                }
            }
        }
        /// <summary>
        /// 测试http代理服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox6.Text) || string.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                Services.T_Proxy.JsonNode_Http(Api.T_Client_Mssql_Api.C1);
                // 执行socks4 代理是否连接成功
                Services.T_Client_Mssql.TestHttp(Api.T_Client_Mssql_Api.C1);
                string[] dataArr = Api.T_Client_Mssql_Api.C_Status.ToString().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var data in dataArr)
                {
                    if (data.Contains("代理连接成功"))
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接成功" + "\r\n" + data + "\r\n");
                    }
                    else
                    {
                        MessageBox.Show(DateTime.Now.ToString() + "：" + "代理连接失败" + "\r\n" + data + "\r\n");
                    }
                }
            }
        }

        private void Proxy_Load(object sender, EventArgs e)
        {
            ReferConfig();

        }
        public void ReferConfig()
        {
            // 读取 app.json 文件内容
            string json = System.IO.File.ReadAllText("Appactions.json");

            // 解析 JSON 数据
            JObject jObject = JObject.Parse(json);

            // 提取值
            string s5 = jObject["socks5"]["ip"].ToString();
            int s51 = Convert.ToInt32(jObject["socks5"]["port"]);

            string s4 = jObject["socks4"]["ip"].ToString();
            int s41 = Convert.ToInt32(jObject["socks4"]["port"]);

            string ht = jObject["http"]["ip"].ToString();
            int ht1 = Convert.ToInt32(jObject["http"]["port"]);

            textBox1.Text = s4;
            textBox2.Text = Convert.ToString(s41.ToString());

            textBox4.Text = s5;
            textBox3.Text = Convert.ToString(s51.ToString());

            textBox6.Text = ht;
            textBox5.Text =Convert.ToString(ht1.ToString());


        }
        /// <summary>
        /// 更新socks4代理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取现有的配置
                string json = System.IO.File.ReadAllText(ConfigFilePath);
                var config = JObject.Parse(json);

                // 更新配置值
                config["socks4"]["ip"] = textBox1.Text;
                config["socks4"]["port"] = textBox2.Text;
                // 保存更新后的配置文件
                System.IO.File.WriteAllText(ConfigFilePath, config.ToString());

                MessageBox.Show("配置已更新!");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新配置失败: {ex.Message}");
            }
        }
        /// <summary>
        /// 更新socks5代理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取现有的配置
                string json = System.IO.File.ReadAllText(ConfigFilePath);
                var config = JObject.Parse(json);

                // 更新配置值
                config["socks5"]["ip"] = textBox4.Text;
                config["socks5"]["port"] = textBox3.Text;
                // 保存更新后的配置文件
                System.IO.File.WriteAllText(ConfigFilePath, config.ToString());

                MessageBox.Show("配置已更新!");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新配置失败: {ex.Message}");
            }
        }
        /// <summary>
        /// 更新http代理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取现有的配置
                string json = System.IO.File.ReadAllText(ConfigFilePath);
                var config = JObject.Parse(json);

                // 更新配置值
                config["http"]["ip"] = textBox6.Text;
                config["http"]["port"] = textBox5.Text;
                // 保存更新后的配置文件
                System.IO.File.WriteAllText(ConfigFilePath, config.ToString());

                MessageBox.Show("配置已更新!");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新配置失败: {ex.Message}");
            }
        }
    }
}
